﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.external.*;
	import flash.net.*;
	import flash.system.*;
	import flash.filters.*;
	import json.*;
	public class Area extends Sprite {
		private var stageW:Number;
		private var stageH:Number;
		private var area:Object;
		//主绘图层
		private var main:Sprite;
		//Tip层
		private var tip:Tip;
		//标记格式
		private var tagFormat:TextFormat;
		//首次运行
		private var one:Boolean = true;
		public function Area() {
			stage.scaleMode = "noScale";
			stage.align = "topLeft";
			stage.addEventListener(Event.RESIZE, resizeHd);
			//添加主绘图层
			main = new Sprite();
			addChild(main);
			//添加Tip层
			tip = new Tip(100, 0);
			addChild(tip);
			//
			init();
		}
		private function init():void {
			var data:String = cV("data");
			if (data) {
				//O.o(str);
				parseData(data);
			} else {
				var url:String = cV("url");
				if (!url) {
					url = "area_data.txt";
				}
				var dataLoader:URLLoader = new URLLoader();
				dataLoader.load(new URLRequest(url));
				dataLoader.addEventListener(IOErrorEvent.IO_ERROR, errorHd);
				dataLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHd);
				dataLoader.addEventListener(Event.COMPLETE, completeHd);
			}
		}
		private function errorHd(e:Event):void {

		}
		private function completeHd(e:Event):void {
			var str:String = e.target.data;
			if (str) {
				//O.o(str);
				//过滤注释行
				str = str.replace(/\/\/.*\s/g,"");
				//过滤回车换行
				str = str.replace(/\n|\r/g,"");
				//O.o(str);
				parseData(str);
			}
		}
		//解析JSON数据
		private function parseData(str:String):void {
			try {
				area = new JSONDecoder(str).getValue();
				//
				//O.o(area.max);
				layout();
			} catch (e:Error) {
				O.o(e);
			}
		}
		private function resizeHd(e:Event):void {
			one = false;
			layout();
		}
		//开始布局绘图
		private function layout():void {
			stageW = stage.stageWidth;
			stageH = stage.stageHeight;
			//
			drawAll();
		}
		private function clearAll():void {
			while (main.numChildren) {
				main.removeChildAt(0);
			}
		}
		private function drawAll():void {
			//清除所有
			clearAll();
			//绘制图表==================================================================
			tagFormat = gF("Verdana",11,parseInt(area.color.replace(/#/g, "0x")));
			//O.o(area.ltrb);
			var left:Number = area.ltrb[0];
			var top:Number = area.ltrb[1];
			var right:Number = area.ltrb[2];
			var bottom:Number = area.ltrb[3];
			//
			var bx:Number = left;
			var by:Number = top;
			var bw:Number = stageW - left - right;
			var bh:Number = stageH - top - bottom;
			var box:Sprite = new Sprite();
			//边框
			box.graphics.lineStyle(1, 0xcccccc);
			box.graphics.drawRect(bx, by, bw, bh);
			//y轴标记 ytag---------------------------------
			box.graphics.lineStyle(1, 0xeeeeee);
			var ytag:Array = area.ytag;
			//O.o(ytag);
			var yl:int = ytag.length;
			if (yl > 1) {
				var yd:Number = bh / (yl - 1);
				for (var i:int = 0; i < yl; i++) {
					var yy:Number = stageH - bottom - yd * i;
					var valign:String = "middle";
					if (i == 0) {
						valign = "bottom";
					} else if (i == yl - 1) {
						valign = "top";
					} else {
						box.graphics.moveTo(left, yy);
						box.graphics.lineTo(left + bw, yy);
					}
					var yt:TextField = getLabel(ytag[i], left - 5, yy - 2, tagFormat, "right", valign);
					box.addChild(yt);
				}
			}
			//x轴标记 xtag----------------------------------
			var xtag:Array = area.xtag;
			//O.o(xtag);
			var xl:int = xtag.length;
			if (yl > 1) {
				var td:Array = new Array();
				var xd:Number = bw / (xl - 1);
				for (var j:int = 0; j < xl; j++) {
					var xx:Number = left + xd * j;
					var xt:TextField = getLabel(xtag[j], xx - 1, stageH - bottom + 2, tagFormat, "center", "top");
					box.addChild(xt);
					td[j] = 0;
				}
				//列表区=====================
				var list:Array = area.list;
				//区块总数
				var ll:int = list.length;
				for (var k:int = 0; k < ll; k++) {
					var sp:Sprite = new Sprite();
					var sc:Number = parseInt(list[k][1].replace(/#/g, "0x"));
					sp.graphics.beginFill(sc);
					var sx:Number;
					var sy:Number;
					for (var ki:int = xl - 1; ki >= 0; ki--) {
						sx = left + xd * ki;
						sy = stageH - bottom - td[ki];
						if (ki == xl - 1) {
							sp.graphics.moveTo(sx, sy);
						} else {
							sp.graphics.lineTo(sx, sy);
						}
					}
					for (var kj:int = 0; kj < xl; kj++) {
						var sh:Number = bh * list[k][2][kj] / area.max;
						td[kj] += sh;
						//
						sx = left + xd * kj;
						sy = stageH - bottom - td[kj];
						sp.graphics.lineTo(sx, sy);
					}
					sp.graphics.endFill();
					sp.name = "sp__" + k;
					sp.buttonMode = true;
					sp.addEventListener(MouseEvent.MOUSE_OVER, overHd);
					sp.addEventListener(MouseEvent.MOUSE_OUT, outHd);
					box.addChild(sp);
				}
			}
			main.addChild(box);
			//信息区=========================================================
			if (area.show_msg) {
				var mr:Number = right*0.08;
				var mh:Number = 24;
				var mw:Number = 12;
				var mx:Number = stageW - right + mr;
				var msg:Sprite = new Sprite();
				for (var mi:int = ll - 1; mi >= 0; mi --) {
					var mc:Number = parseInt(list[mi][1].replace(/#/g, "0x"));
					var my:Number = (ll - mi) * mh - mh/2;
					var dot:Sprite = new Sprite();
					dot.graphics.beginFill(mc);
					dot.graphics.drawRect(mx, my - mw/2, mw, mw);
					dot.graphics.endFill();
					msg.addChild(dot);
					var mt:TextField = getLabel(list[mi][0], mx + mw + 5, my - 3, tagFormat, "left");
					msg.addChild(mt);
				}
				msg.y = top + (bh - mh * ll)/2;
				main.addChild(msg);
			}
		}
		//
		private function overHd(e:MouseEvent):void {
			e.target.alpha = 0.8;
			var i:int = parseInt(e.target.name.split("__")[1]);
			var str:String = area.list[i][0];
			tip.show(str);
		}
		private function outHd(e:MouseEvent):void {
			e.target.alpha = 1;
			tip.hide();
		}
		//新建标记
		private function getLabel(s:String, tx:int, ty:int, tf:TextFormat, align:String = "center", valign:String = "middle"):TextField {
			var label:TextField = new TextField();
			label.selectable = false;
			label.mouseEnabled = false;
			label.autoSize = "left";
			label.htmlText = s;
			label.setTextFormat(tf);
			if (align == "left") {
				label.x = tx;
			} else if (align == "right") {
				label.x = tx - label.textWidth;
			} else {
				label.x = tx - label.textWidth/2;
			}
			if (valign == "top") {
				label.y = ty;
			} else if (valign == "bottom") {
				label.y = ty - label.textHeight;
			} else {
				label.y = ty - label.textHeight/2;
			}
			return label;
		}
		//标记格式
		private function gF(font:String = "Verdana", size:Number = 12, color:Number = 0):TextFormat {
			var tf:TextFormat = new TextFormat();
			tf.font = font;
			tf.size = size;
			tf.color = color;
			return tf;
		}
		//读取传入参数
		public function cV(str:String):String {
			return this.root.loaderInfo.parameters[str];
		}
	}
}